﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DevExExpressionSummary.Default" %>

<%@ Register Assembly="DevExpress.Web.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxRoundPanel" TagPrefix="dx" %>

<%@ Register Assembly="DevExpress.XtraReports.v12.1.Web, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.XtraReports.Web" TagPrefix="dx" %>

<%@ Register assembly="ASPxGridViewExpressionSummary" namespace="ASPxGridViewExpressionSummary" tagprefix="cc1" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dx" %>

<%@ Register assembly="DevExpress.Web.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxPopupControl" tagprefix="dx" %>

<%@ Register assembly="DevExpress.Web.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxPanel" tagprefix="dx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Expression Summary in Grid Demo</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
        <script type="text/javascript">
            function OnCalculateStaticExpression() {
                var variables = $(".variable", "#variblesContainer")
                .map(function (index, varHolder) {
                    var v = new DevExExpressionSummary.VariableDTO();
                    v.Name = $($('div', varHolder)[0]).text();
                    v.Value = $($('div', varHolder)[1]).text();
                    return v;
                });

                PageMethods.EvaluateStatic(beExpression.GetText(), variables.toArray(), OnStaticExpressionEvaluated, OnStaticExpressionEvalError);
            }

            function OnStaticExpressionEvaluated(res) {
                lblStaticExpressionRes.SetText(res)
            }

            function OnStaticExpressionEvalError(error) {
                if (error != null) {
                    lblStaticExpressionRes.SetText("Error:" + error.get_message());
                }
            }

            function AddVariable() {
                var varCount = $(".variable", "#variblesContainer").length; // get number of existing variables
                txtVarName.SetText('Var' + varCount);
                txtValue.SetText('0');
                variableEditor.Show();
                variableEditor.VarAction = 'New';
            }

            function OnDilogCancel() {
                variableEditor.Hide();
            }

            function OnDialogOk() {
                if (variableEditor.VarAction == 'Edit') {
                    var container = variableEditor.VarContainer;
                    var varHolder = $('div', container);
                    var name = $(varHolder[0]);
                    var val = $(varHolder[1]);
                    name.text(txtVarName.GetText());
                    val.text(txtValue.GetText());
                }

                if (variableEditor.VarAction == 'New') {
                    CreateVariable(txtVarName.GetText(), txtValue.GetText());                
                }
                variableEditor.Hide();
            }

            function CreateVariable(name, value) {
                var newVar = $(
                      "<div class='variable'>"
                        + "<div style='float:left; width:50px'>" + name + "</div>"
                        + "<div style='float:left; width:50px'>" + value + "</div>"
                        + "<a href='#'>Edit</a>&nbsp;"
                        + "<a href='#'>Delete</a>"
                    + "</div>");
                var mainContainer = $("#variblesContainer");
                newVar.appendTo(mainContainer);
                var a = $('A', newVar);
                
                // add edit handler
                $(a[0]).bind("click", OnEditVarClick);

                // add remove handler
                $(a[1]).bind("click", OnDeleteVarClick);
            }

            function OnEditVarClick() {
                variableEditor.VarContainer = $(this).parent();
                var varHolder = $($('div', $(this).parent()));
                var name = $(varHolder[0]);
                var val = $(varHolder[1]);
                txtVarName.SetText(name.text());
                txtValue.SetText(val.text());
                variableEditor.VarAction = 'Edit'
                variableEditor.Show();
                return false;               
            }

            function OnDeleteVarClick() {
                $(this).parent().remove();
                return false;
            }
        </script>
        <asp:ScriptManager EnablePageMethods="true" runat="server">
        </asp:ScriptManager>
        <div>
            <b>Expression Evaluator</b>
            <br />
            <div style="width:320px;float:left">
                <dx:ASPxButtonEdit ID="beExpression" ClientInstanceName="beExpression" runat="server" Height="16px" 
                    AutoPostBack="false" Text="0" Width="319px">
                    <Buttons>
                        <dx:EditButton Text="=">
                        </dx:EditButton>
                    </Buttons>
                    <ClientSideEvents ButtonClick="OnCalculateStaticExpression" />
                </dx:ASPxButtonEdit>
            </div>
            <div style="float:left; padding:3px 0px 0px 20px">
                <dx:ASPxLabel ID="lblStaticExpressionRes" ClientInstanceName="lblStaticExpressionRes" runat="server" Text="Result">
                </dx:ASPxLabel>
            </div>
            <br />
            <br />
            <dx:ASPxRoundPanel runat="server">
                <PanelCollection>
                    <dx:PanelContent runat="server">
                        <div id="variblesContainer">
                        </div>
                    </dx:PanelContent>
                </PanelCollection>
                <HeaderTemplate>
                    <b>Variables.</b><a onclick="AddVariable(); return false;" style="margin-left:60px" href="">Add New</a>   
                </HeaderTemplate>
            </dx:ASPxRoundPanel>
            
            <dx:ASPxPopupControl HeaderText="Edit Variable" ClientInstanceName="variableEditor" runat="server" 
                AutoUpdatePosition="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter">
                <ContentCollection>
                    <dx:PopupControlContentControl ID="pccConfirmationDialog" runat="server" SupportsDisabledAttribute="True">
                        <table>
                            <tr>
                                <td><dx:ASPxLabel ID="lblVarName" runat="server" Text="Variable Name:"/></td>
                                <td><dx:ASPxTextBox ID="txtVarName" ClientInstanceName="txtVarName" runat="server" Width="70px"/></td>
                            </tr>
                            <tr>
                                <td><dx:ASPxLabel ID="ASPxLabel1" runat="server" Text="Value:"/></td>
                                <td><dx:ASPxTextBox ID="ASPxTextBox1" ClientInstanceName="txtValue" runat="server" Width="70px"/></td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <div style="float:right;">
                                        <dx:ASPxButton ID="ASPxButton1" Width="60" AutoPostBack="false" ClientSideEvents-Click="OnDilogCancel" runat="server" Text="Cancel">
                                        </dx:ASPxButton>
                                    </div>
                                    <div style="float:right;width:65px;">
                                        <dx:ASPxButton ID="ASPxButton2" AutoPostBack="false" Width="60" ClientSideEvents-Click="OnDialogOk" runat="server" Text="OK">
                                        </dx:ASPxButton>           
                                    </div> 
                                </td>
                            </tr>
                        </table>                        
                     </dx:PopupControlContentControl>
                </ContentCollection>
            </dx:ASPxPopupControl>
            <br />
            <hr />
            <br />
            <b>Expression summary in ASPxGridView</b>
            <br />
            <cc1:ExpressionASPxGridView ID="gvData" runat="server" 
                AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
                <Columns>
                    
                    <dx:GridViewDataSpinEditColumn Caption="Column1" FieldName="Column1" Name="Column1" VisibleIndex="0"/>
                    <dx:GridViewDataSpinEditColumn Caption="Column2" FieldName="Column2" Name="Column2" VisibleIndex="1"/>
                    <dx:GridViewDataSpinEditColumn Caption="Column3" FieldName="Column3" Name="Column3" VisibleIndex="2"/>
                    <dx:GridViewDataSpinEditColumn Caption="Column4" FieldName="Column4" Name="Column4" VisibleIndex="3"/>
                    <dx:GridViewDataSpinEditColumn Caption="Column5" FieldName="Column5" Name="Column5" VisibleIndex="4"/>
                    <dx:GridViewDataSpinEditColumn Caption="Column6" FieldName="Column6" Name="Column6" VisibleIndex="5"/>
                    
                </Columns>
                <TotalSummary>
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column1" Expression="SUM(Column1)" ValueDisplayFormat="SUM(Column1) = {0}" />
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column2" Expression="(SUM(Column3)-SUM(Column2))/COUNT()" ValueDisplayFormat="(SUM(Column3)-SUM(Column2))/COUNT() = {0}" />
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column3" Expression="SUM((Column3 + Column2)/2)" ValueDisplayFormat="SUM((Column3 + Column2)/2) = {0}" />
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column4" Expression="AVG(Column4/Column3)" ValueDisplayFormat="AVG(Column4/Column3)={0:N2}" />
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column5" Expression="MIN(Column5+1)+2" ValueDisplayFormat="MIN(Column5+1)+2={0}" />
                    <cc1:ExpressionSummary FieldName="Column1" ShowInColumn="Column6" Expression="MAX(Column6-1)-2" ValueDisplayFormat="MAX(Column6-1)-2={0}" />
                </TotalSummary>
                <SettingsPager Visible="False">
                </SettingsPager>
                <Settings ShowFooter="True" 
                    ShowGroupFooter="VisibleAlways" />
            </cc1:ExpressionASPxGridView>
    
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                SelectMethod="Generate" TypeName="DevExExpressionSummary.DemoData">
            </asp:ObjectDataSource>
            <br />
            <hr />
            <br />
            <b><span>Expression summary in XtrsReports</span></b>
            <br />
            <dx:ReportViewer id="ReportViewer1" PageByPage="False" runat="server">
            </dx:ReportViewer>
        </div>
    </form>
</body>
</html>
